package com.wocai.jrt.orgadmin.stats.service.impl;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import com.vteba.utils.common.BigDecimalUtils;
import com.wocai.jrt.admin.common.cache.Const;
import com.wocai.jrt.orgadmin.stats.service.spi.GroupPerformanceStatsService;
import com.wocai.jrt.orgadmin.stats.service.spi.RedisCacheGroupStatsService;

@Named
public class RedisCacheGroupStatsServiceImpl implements RedisCacheGroupStatsService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(RedisCacheGroupStatsServiceImpl.class);

	@Inject
	private GroupPerformanceStatsService groupPerformanceStatsServiceImpl;

	@Override
	@CachePut(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_APPOINTMENT + "+ #groupId"
			+ "+ '_'" + "+ #date")
	public Map<String, Object> queryGroupAppointmentPerformanceCachePut(String groupId, String date) {
		LOGGER.info("更新缓存:GroupAppointmentPerformance");
		return groupPerformanceStatsServiceImpl.groupAppointmentPerformance(groupId, date);
	}

	@Override
	@CachePut(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_INVERSTOR + "+ #groupId"
			+ "+ '_'" + "+ #date")
	public Map<String, Object> queryGroupinvestorPerformanceCachePut(String groupId, String date) {
		LOGGER.info("更新缓存:GroupinvestorPerformance");
		return groupPerformanceStatsServiceImpl.groupinvestorPerformance(groupId, date);
	}

	@Override
	@CachePut(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_SALES + "+ #groupId" + "+ '_'"
			+ "+ #date")
	public Map<String, Object> queryGroupSalesPerformanceCachePut(String groupId, String date) {
		LOGGER.info("更新缓存:GroupSalesPerformance");
		return groupPerformanceStatsServiceImpl.groupSalesPerformance(groupId, date);
	}

	@Override
	@Cacheable(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_SALES + "+ #groupId" + "+ '_'"
			+ "+ #date")
	public Map<String, Object> queryGroupSalesPerformance(String groupId, String date) {
		return groupPerformanceStatsServiceImpl.groupSalesPerformance(groupId, date);
	}

	@Override
	@Cacheable(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_INVERSTOR + "+ #groupId"
			+ "+ '_'" + "+ #date")
	public Map<String, Object> queryGroupinvestorPerformance(String groupId, String date) {
		return groupPerformanceStatsServiceImpl.groupinvestorPerformance(groupId, date);
	}

	@Override
	@Cacheable(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_APPOINTMENT + "+ #groupId"
			+ "+ '_'" + "+ #date")
	public Map<String, Object> queryGroupAppointmentPerformance(String groupId, String date) {
		return groupPerformanceStatsServiceImpl.groupAppointmentPerformance(groupId, date);
	}

	@Override
	@Cacheable(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_SALES + "+ #groupId" + "+ '_'"
			+ "+ #date")
	public Double queryGroupSalesPerformanceYear(String groupId, String date) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy");
		DateTimeFormatter formatterMonth = DateTimeFormat.forPattern("yyyy-MM");
		DateTime startTime = DateTime.parse(date, formatter);
		LOGGER.info("GroupStatsServiceImpl queryGroupSalesPerformance, startTime=[{}]", startTime);
		// 统计订单,从缓存中取数据
		Double sum = 0D;
		for (int i = 0; i < 12; i++) {
			Map<String, Object> map = groupPerformanceStatsServiceImpl.groupSalesPerformance(
					groupId, startTime.toString(formatterMonth));
			@SuppressWarnings("unchecked")
			List<Double> redisData = (List<Double>) map.get("data");
			for (Double double1 : redisData) {
				sum = BigDecimalUtils.add(sum, double1);
			}
			startTime = startTime.plusMonths(1);
		}
		return sum;
	}

	@Override
	@Cacheable(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_INVERSTOR + "+ #groupId"
			+ "+ '_'" + "+ #date")
	public Integer queryGroupinvestorPerformanceYear(String groupId, String date) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy");
		DateTimeFormatter formatterMonth = DateTimeFormat.forPattern("yyyy-MM");
		DateTime startTime = DateTime.parse(date, formatter);
		LOGGER.info("GroupStatsServiceImpl queryGroupSalesPerformance, startTime=[{}]", startTime);
		// 统计订单,从缓存中取数据
		Integer sum = 0;
		for (int i = 0; i < 12; i++) {
			Map<String, Object> map = groupPerformanceStatsServiceImpl.groupinvestorPerformance(
					groupId, startTime.toString(formatterMonth));
			@SuppressWarnings("unchecked")
			List<Integer> redisData = (List<Integer>) map.get("data");
			for (Integer integer : redisData) {
				sum = sum + integer;
			}
			startTime = startTime.plusMonths(1);
		}
		return sum;
	}

	@Override
	@Cacheable(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_APPOINTMENT + "+ #groupId"
			+ "+ '_'" + "+ #date")
	public Integer queryGroupAppointmentPerformanceYear(String groupId, String date) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy");
		DateTimeFormatter formatterMonth = DateTimeFormat.forPattern("yyyy-MM");
		DateTime startTime = DateTime.parse(date, formatter);
		LOGGER.info("GroupStatsServiceImpl queryGroupSalesPerformance, startTime=[{}]", startTime);
		// 统计订单,从缓存中取数据
		Integer sum = 0;
		for (int i = 0; i < 12; i++) {
			Map<String, Object> map = groupPerformanceStatsServiceImpl.groupAppointmentPerformance(
					groupId, startTime.toString(formatterMonth));
			@SuppressWarnings("unchecked")
			List<Integer> redisData = (List<Integer>) map.get("data");
			for (Integer integer : redisData) {
				sum = sum + integer;
			}
			startTime = startTime.plusMonths(1);
		}
		return sum;
	}

	@Override
	@CachePut(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_SALES + "+ #groupId" + "+ '_'"
			+ "+ #date")
	public Double queryGroupSalesPerformanceYearCachePut(String groupId, String date) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy");
		DateTimeFormatter formatterMonth = DateTimeFormat.forPattern("yyyy-MM");
		DateTime startTime = DateTime.parse(date, formatter);
		LOGGER.info("GroupStatsServiceImpl queryGroupSalesPerformance, startTime=[{}]", startTime);
		// 统计订单,从缓存中取数据
		Double sum = 0D;
		for (int i = 0; i < 12; i++) {
			Map<String, Object> map = groupPerformanceStatsServiceImpl.groupSalesPerformance(
					groupId, startTime.toString(formatterMonth));
			@SuppressWarnings("unchecked")
			List<Double> redisData = (List<Double>) map.get("data");
			for (Double double1 : redisData) {
				sum = BigDecimalUtils.add(sum, double1);
			}
			startTime = startTime.plusMonths(1);
		}
		return sum;
	}

	@Override
	@CachePut(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_INVERSTOR + "+ #groupId"
			+ "+ '_'" + "+ #date")
	public Integer queryGroupinvestorPerformanceYearCachePut(String groupId, String date) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy");
		DateTimeFormatter formatterMonth = DateTimeFormat.forPattern("yyyy-MM");
		DateTime startTime = DateTime.parse(date, formatter);
		LOGGER.info("GroupStatsServiceImpl queryGroupSalesPerformance, startTime=[{}]", startTime);
		// 统计订单,从缓存中取数据
		Integer sum = 0;
		for (int i = 0; i < 12; i++) {
			Map<String, Object> map = groupPerformanceStatsServiceImpl.groupinvestorPerformance(
					groupId, startTime.toString(formatterMonth));
			@SuppressWarnings("unchecked")
			List<Integer> redisData = (List<Integer>) map.get("data");
			for (Integer integer : redisData) {
				sum = sum + integer;
			}
			startTime = startTime.plusMonths(1);
		}
		return sum;
	}

	@Override
	@CachePut(value = Const.STATS, key = Const.PREFIX_GROUP_STATS_APPOINTMENT + "+ #groupId"
			+ "+ '_'" + "+ #date")
	public Integer queryGroupAppointmentPerformanceYearCachePut(String groupId, String date) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy");
		DateTimeFormatter formatterMonth = DateTimeFormat.forPattern("yyyy-MM");
		DateTime startTime = DateTime.parse(date, formatter);
		LOGGER.info("GroupStatsServiceImpl queryGroupSalesPerformance, startTime=[{}]", startTime);
		// 统计订单,从缓存中取数据
		Integer sum = 0;
		for (int i = 0; i < 12; i++) {
			Map<String, Object> map = groupPerformanceStatsServiceImpl.groupAppointmentPerformance(
					groupId, startTime.toString(formatterMonth));
			@SuppressWarnings("unchecked")
			List<Integer> redisData = (List<Integer>) map.get("data");
			for (Integer integer : redisData) {
				sum = sum + integer;
			}
			startTime = startTime.plusMonths(1);
		}
		return sum;
	}
}
